#ifndef SIMULATOR_H_
#define SIMULATOR_H_

#include <assert.h>
#include <deque>
#include <pthread.h>
#include "../base/base.h"
#include "../tac/tac.h"

#define		TAC_STATUS_ONLINE		0
#define		TAC_STATUS_OFFLINE		1
#define		TAC_STATUS_CONNECTING	2

#define		TAC_CHECKTIMES			3
#define 	TAC_ITV					1000

typedef struct
{
	uint32	nID;
	uint8	nDow;
	char8 	Time[10];
	BYTE 	Data[512];
	char8 	Comment[128];
}CASE;

typedef std::deque<CASE *> CaseList;

class Simulator
	: public TAC
	, public DB
{
public:
	Simulator();
	~Simulator();
	
public:
	BaseCB *pCallBack;
	void invoke(void *p)
	{
		(*pCallBack)(p);
	}
	
	BaseCB *pCallSend;
	void invokeSend(void *p)
	{
		(*pCallSend)(p);
	}
	
	void SetLoop(int32 nLoop)
	{m_nLoop = nLoop;}
	
	int32 GetLoop(void) const
	{return m_nLoop;}
	
	void SetInterval(uint32 nItv)
	{m_nInterval = nItv;}
	
	uint32 GetInterval(void) const
	{return m_nInterval;}
	
	int32 Init();
	int32 Start();
	int32 Stop();
	int32 StartSend();
	int32 StopSend();
	void ClearCase();
	int32 GetCase(int nDow, CaseList *pList);
	int32 LoadCase(int nDow = -1);
	int32 AddCase(const CASE *pCase);
	int32 DeleteCase(uint32 nID);
	int32 EditCase(const CASE *pCase);
	CASE *EnumCase();
	
private:
	bool IsSend(void) const
	{return m_bSend;}
	
	bool IsStart(void) const
	{return m_bStart;}
	
	int32 GetStatus() const
	{return m_nStatus;}
	
	void SetStatus(int32 nStatus);
	
	static void *thStart(void *pData);
	static void *thSend(void *pData);
	static void *thCheckStatus(void *pData);
	
	CASE *FindCaseByTime(const char *pTime);
	
private:
	bool	m_bSend;
	bool	m_bStart;
	
	CaseList *m_pCaseList;
	
	uint32 	m_nInterval;
	int32  	m_nLoop;
	int32	m_nStatus;
};

#endif